package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SignLog;
import com.ruoyi.common.utils.Base64Utils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.jm.GmUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.core.domain.SignLogVo;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SignLogMapper;

import javax.annotation.Resource;

/**
 * 签章日志Service业务层处理
 *
 * @author ess
 * @date 2021-10-28
 */
@Service
public class SignLogServiceImpl implements ISignLogService {
    @Resource
    private SignLogMapper signLogMapper;

    @Autowired
    private IPersonService personService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private IBusinessSysService businessSysService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ISealInfoService sealInfoService;


    /**
     * 查询签章日志
     *
     * @param signLogId 签章日志主键
     * @return 签章日志
     */
    @Override
    public SignLog selectSignLogBySignLogId(String signLogId) {
        return signLogMapper.selectSignLogBySignLogId(signLogId);
    }

    /**
     * 查询签章日志列表
     *
     * @param signLog 签章日志
     * @return 签章日志
     */
    @Override
    public List<SignLog> selectSignLogList(SignLog signLog) {

        ArrayList<SignLog> signLogArrayList = new ArrayList<>();

//        印章名称查找印章
        if (StringUtils.isNotEmpty(signLog.getSealId())) {
            List<SealInfo> sealInfos = sealInfoService.selectSealInfoBySealName(signLog.getSealId());
            if (sealInfos != null && sealInfos.size() > 0) {
                for (SealInfo sealInfo : sealInfos) {
                    if (sealInfo.getState() != 2) {
                        signLog.setSealId(sealInfo.getSealInfoId());
                    }
                }
                if (StringUtils.isEmpty(signLog.getSealId())) {
                    signLog.setSealId(sealInfos.get(0).getSealInfoId());
                }
            }
        }

        if (StringUtils.isNotEmpty(signLog.getSignUserName()) ) {
                List<Person> personList = personService.selectPersonListByPersonName(signLog.getSignUserName());
                if (personList != null && personList.size() > 0) {
                    for (Person person : personList) {
                        signLog.setSignUserId(person.getPersonId());
                        List<SignLog> signLogs = signLogMapper.selectSignLogList(signLog);
                        if (signLogs != null && signLogs.size() > 0) {
                            signLogArrayList.addAll(signLogs);
                        }
                    }
                }

                return takeSignLogName(signLogArrayList);
            }


//        //sign_user_id  格式为 姓名-生日
//        if (StringUtils.isNotEmpty(signLog.getSignUserId()) && signLog.getSignUserId().contains("-")) {
//            Person person = new Person();
//            person.setBirNum(signLog.getSignUserId().split("-")[1]);
//            person.setPersonName(signLog.getSignUserId().split("-")[0]);
//            List<Person> people = personService.selectPersonList(person);
//            if (people != null && people.size() > 0) {
//                signLog.setSignUserId(people.get(0).getPersonId());
//            }
//        }



        return takeSignLogName(signLogMapper.selectSignLogList(signLog));
    }

    /**
     * 新增签章日志
     *
     * @param signLog 签章日志
     * @return 结果
     */
    @Override
    public int insertSignLog(SignLog signLog) {
        signLog.setSignLogId(IdUtils.simpleUUID());
        signLog.setSignTime(DateUtils.getTime());
//        signLog.setTerminalType("服务器");
        signLog.setTerminalType("linux");
        String loginIndoHash = Base64Utils.ESSGetBase64Encode(GmUtil.sm3(signLog.toString().getBytes()));
        signLog.setSafeHash(loginIndoHash);
        return signLogMapper.insertSignLog(signLog);
    }

    /**
     * 修改签章日志
     *
     * @param signLog 签章日志
     * @return 结果
     */
    @Override
    public int updateSignLog(SignLog signLog) {
        return signLogMapper.updateSignLog(signLog);
    }

    /**
     * 批量删除签章日志
     *
     * @param signLogIds 需要删除的签章日志主键
     * @return 结果
     */
    @Override
    public int deleteSignLogBySignLogIds(String[] signLogIds) {
        return signLogMapper.deleteSignLogBySignLogIds(signLogIds);
    }

    /**
     * 删除签章日志信息
     *
     * @param signLogId 签章日志主键
     * @return 结果
     */
    @Override
    public int deleteSignLogBySignLogId(String signLogId) {
        return signLogMapper.deleteSignLogBySignLogId(signLogId);
    }

    /**
     * 获取详细字段名称
     */
    private List<SignLog> takeSignLogName(List<SignLog> signLogList) {
        if (signLogList != null && signLogList.size() > 0) {
            for (SignLog signLog : signLogList) {
                SignLogVo SignLogVo = new SignLogVo();
                if (signLog.getSignUserId() != null && !"".equals(signLog.getSignUserId())) {
                    Person person = personService.selectPersonByPersonId(signLog.getSignUserId());
                    if (person != null) {
                        SignLogVo.setSignUserName(person.getPersonName() + "-" + person.getBirNum().substring(4));
                    }
                }
                if (signLog.getUnitId() != null && !"".equals(signLog.getUnitId())) {
                    SysDept sysDept = deptService.selectDeptById(Long.valueOf(signLog.getUnitId()));
                    if (sysDept != null) {
                        SignLogVo.setUnitName(sysDept.getDeptName());
                    }
                }

                if (signLog.getBusinessSysId() != null && !"".equals(signLog.getBusinessSysId())) {
                    BusinessSys businessSys = businessSysService.selectBusinessSysByBSysId(signLog.getBusinessSysId());
                    if (businessSys != null) {
                        SignLogVo.setBusinessSysName(businessSys.getbName());
                    }

                }

                if (signLog.getSealId() != null && !"".equals(signLog.getSealId())) {
                    SealInfo sealInfo = sealInfoService.selectSealInfoBySealInfoId(signLog.getSealId());
                    if (sealInfo != null) {
                        SignLogVo.setSealName(sealInfo.getSealName());
                        signLog.setSealType(sealInfo.getSealDetail().getSealTypeId());
                    } else {
                        SignLogVo.setSealName(Constants.ERR_NAME);
                    }
                }
                signLog.setSignLogVo(SignLogVo);
            }
        }
        return signLogList;
    }
}
